{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date                   122\n",
      "Day                    122\n",
      "Cases_Guinea            93\n",
      "Cases_Liberia           83\n",
      "Cases_SierraLeone       87\n",
      "Cases_Nigeria           38\n",
      "Cases_Senegal           25\n",
      "Cases_UnitedStates      18\n",
      "Cases_Spain             16\n",
      "Cases_Mali              12\n",
      "Deaths_Guinea           92\n",
      "Deaths_Liberia          81\n",
      "Deaths_SierraLeone      87\n",
      "Deaths_Nigeria          38\n",
      "Deaths_Senegal          22\n",
      "Deaths_UnitedStates     18\n",
      "Deaths_Spain            16\n",
      "Deaths_Mali             12\n",
      "dtype: int64\n",
      "/n          Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone  \\\n",
      "0    1/5/2015  289        2776.0            NaN            10030.0   \n",
      "1    1/4/2015  288        2775.0            NaN             9780.0   \n",
      "2    1/3/2015  287        2769.0         8166.0             9722.0   \n",
      "3    1/2/2015  286           NaN         8157.0                NaN   \n",
      "4  12/31/2014  284        2730.0         8115.0             9633.0   \n",
      "\n",
      "   Cases_Nigeria  Cases_Senegal  Cases_UnitedStates  Cases_Spain  Cases_Mali  \\\n",
      "0            NaN            NaN                 NaN          NaN         NaN   \n",
      "1            NaN            NaN                 NaN          NaN         NaN   \n",
      "2            NaN            NaN                 NaN          NaN         NaN   \n",
      "3            NaN            NaN                 NaN          NaN         NaN   \n",
      "4            NaN            NaN                 NaN          NaN         NaN   \n",
      "\n",
      "   Deaths_Guinea  Deaths_Liberia  Deaths_SierraLeone  Deaths_Nigeria  \\\n",
      "0         1786.0             NaN              2977.0             NaN   \n",
      "1         1781.0             NaN              2943.0             NaN   \n",
      "2         1767.0          3496.0              2915.0             NaN   \n",
      "3            NaN          3496.0                 NaN             NaN   \n",
      "4         1739.0          3471.0              2827.0             NaN   \n",
      "\n",
      "   Deaths_Senegal  Deaths_UnitedStates  Deaths_Spain  Deaths_Mali  \n",
      "0             NaN                  NaN           NaN          NaN  \n",
      "1             NaN                  NaN           NaN          NaN  \n",
      "2             NaN                  NaN           NaN          NaN  \n",
      "3             NaN                  NaN           NaN          NaN  \n",
      "4             NaN                  NaN           NaN          NaN  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "ebola = pd.read_csv('../data/country_timeseries.csv')\n",
    "print(ebola.count())\n",
    "print('/n', ebola.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Date    Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone  \\\n",
      "0  False  False         False           True              False   \n",
      "1  False  False         False           True              False   \n",
      "2  False  False         False          False              False   \n",
      "3  False  False          True          False               True   \n",
      "4  False  False         False          False              False   \n",
      "\n",
      "   Cases_Nigeria  Cases_Senegal  Cases_UnitedStates  Cases_Spain  Cases_Mali  \\\n",
      "0           True           True                True         True        True   \n",
      "1           True           True                True         True        True   \n",
      "2           True           True                True         True        True   \n",
      "3           True           True                True         True        True   \n",
      "4           True           True                True         True        True   \n",
      "\n",
      "   Deaths_Guinea  Deaths_Liberia  Deaths_SierraLeone  Deaths_Nigeria  \\\n",
      "0          False            True               False            True   \n",
      "1          False            True               False            True   \n",
      "2          False           False               False            True   \n",
      "3           True           False                True            True   \n",
      "4          False           False               False            True   \n",
      "\n",
      "   Deaths_Senegal  Deaths_UnitedStates  Deaths_Spain  Deaths_Mali  \n",
      "0            True                 True          True         True  \n",
      "1            True                 True          True         True  \n",
      "2            True                 True          True         True  \n",
      "3            True                 True          True         True  \n",
      "4            True                 True          True         True  \n",
      "1214\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(ebola.isnull().head())\n",
    "print(np.count_nonzero(ebola.isnull()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone\n",
      "0    1/5/2015  289        2776.0            0.0            10030.0\n",
      "1    1/4/2015  288        2775.0            0.0             9780.0\n",
      "2    1/3/2015  287        2769.0         8166.0             9722.0\n",
      "3    1/2/2015  286           0.0         8157.0                0.0\n",
      "4  12/31/2014  284        2730.0         8115.0             9633.0\n",
      "5  12/28/2014  281        2706.0         8018.0             9446.0\n",
      "6  12/27/2014  280        2695.0            0.0             9409.0\n",
      "7  12/24/2014  277        2630.0         7977.0             9203.0\n",
      "8  12/21/2014  273        2597.0            0.0             9004.0\n",
      "9  12/20/2014  272        2571.0         7862.0             8939.0\n",
      "Help on method fillna in module pandas.core.frame:\n",
      "\n",
      "fillna(value: 'Hashable | Mapping | Series | DataFrame' = None, *, method: 'FillnaOptions | None' = None, axis: 'Axis | None' = None, inplace: 'bool' = False, limit: 'int | None' = None, downcast: 'dict | None' = None) -> 'DataFrame | None' method of pandas.core.frame.DataFrame instance\n",
      "    Fill NA/NaN values using the specified method.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    value : scalar, dict, Series, or DataFrame\n",
      "        Value to use to fill holes (e.g. 0), alternately a\n",
      "        dict/Series/DataFrame of values specifying which value to use for\n",
      "        each index (for a Series) or column (for a DataFrame).  Values not\n",
      "        in the dict/Series/DataFrame will not be filled. This value cannot\n",
      "        be a list.\n",
      "    method : {'backfill', 'bfill', 'ffill', None}, default None\n",
      "        Method to use for filling holes in reindexed Series:\n",
      "    \n",
      "        * ffill: propagate last valid observation forward to next valid.\n",
      "        * backfill / bfill: use next valid observation to fill gap.\n",
      "    \n",
      "    axis : {0 or 'index', 1 or 'columns'}\n",
      "        Axis along which to fill missing values. For `Series`\n",
      "        this parameter is unused and defaults to 0.\n",
      "    inplace : bool, default False\n",
      "        If True, fill in-place. Note: this will modify any\n",
      "        other views on this object (e.g., a no-copy slice for a column in a\n",
      "        DataFrame).\n",
      "    limit : int, default None\n",
      "        If method is specified, this is the maximum number of consecutive\n",
      "        NaN values to forward/backward fill. In other words, if there is\n",
      "        a gap with more than this number of consecutive NaNs, it will only\n",
      "        be partially filled. If method is not specified, this is the\n",
      "        maximum number of entries along the entire axis where NaNs will be\n",
      "        filled. Must be greater than 0 if not None.\n",
      "    downcast : dict, default is None\n",
      "        A dict of item->dtype of what to downcast if possible,\n",
      "        or the string 'infer' which will try to downcast to an appropriate\n",
      "        equal type (e.g. float64 to int64 if possible).\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    DataFrame or None\n",
      "        Object with missing values filled or None if ``inplace=True``.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    interpolate : Fill NaN values using interpolation.\n",
      "    reindex : Conform object to new index.\n",
      "    asfreq : Convert TimeSeries to specified frequency.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],\n",
      "    ...                    [3, 4, np.nan, 1],\n",
      "    ...                    [np.nan, np.nan, np.nan, np.nan],\n",
      "    ...                    [np.nan, 3, np.nan, 4]],\n",
      "    ...                   columns=list(\"ABCD\"))\n",
      "    >>> df\n",
      "         A    B   C    D\n",
      "    0  NaN  2.0 NaN  0.0\n",
      "    1  3.0  4.0 NaN  1.0\n",
      "    2  NaN  NaN NaN  NaN\n",
      "    3  NaN  3.0 NaN  4.0\n",
      "    \n",
      "    Replace all NaN elements with 0s.\n",
      "    \n",
      "    >>> df.fillna(0)\n",
      "         A    B    C    D\n",
      "    0  0.0  2.0  0.0  0.0\n",
      "    1  3.0  4.0  0.0  1.0\n",
      "    2  0.0  0.0  0.0  0.0\n",
      "    3  0.0  3.0  0.0  4.0\n",
      "    \n",
      "    We can also propagate non-null values forward or backward.\n",
      "    \n",
      "    >>> df.fillna(method=\"ffill\")\n",
      "         A    B   C    D\n",
      "    0  NaN  2.0 NaN  0.0\n",
      "    1  3.0  4.0 NaN  1.0\n",
      "    2  3.0  4.0 NaN  1.0\n",
      "    3  3.0  3.0 NaN  4.0\n",
      "    \n",
      "    Replace all NaN elements in column 'A', 'B', 'C', and 'D', with 0, 1,\n",
      "    2, and 3 respectively.\n",
      "    \n",
      "    >>> values = {\"A\": 0, \"B\": 1, \"C\": 2, \"D\": 3}\n",
      "    >>> df.fillna(value=values)\n",
      "         A    B    C    D\n",
      "    0  0.0  2.0  2.0  0.0\n",
      "    1  3.0  4.0  2.0  1.0\n",
      "    2  0.0  1.0  2.0  3.0\n",
      "    3  0.0  3.0  2.0  4.0\n",
      "    \n",
      "    Only replace the first NaN element.\n",
      "    \n",
      "    >>> df.fillna(value=values, limit=1)\n",
      "         A    B    C    D\n",
      "    0  0.0  2.0  2.0  0.0\n",
      "    1  3.0  4.0  NaN  1.0\n",
      "    2  NaN  1.0  NaN  3.0\n",
      "    3  NaN  3.0  NaN  4.0\n",
      "    \n",
      "    When filling using a DataFrame, replacement happens along\n",
      "    the same column names and same indices\n",
      "    \n",
      "    >>> df2 = pd.DataFrame(np.zeros((4, 4)), columns=list(\"ABCE\"))\n",
      "    >>> df.fillna(df2)\n",
      "         A    B    C    D\n",
      "    0  0.0  2.0  0.0  0.0\n",
      "    1  3.0  4.0  0.0  1.0\n",
      "    2  0.0  0.0  0.0  NaN\n",
      "    3  0.0  3.0  0.0  4.0\n",
      "    \n",
      "    Note that column D is not affected since it is not present in df2.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(ebola.fillna(0).iloc[0:10, 0:5])\n",
    "help(ebola.fillna)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone\n",
      "0    1/5/2015  289        2776.0            NaN            10030.0\n",
      "1    1/4/2015  288        2775.0            NaN             9780.0\n",
      "2    1/3/2015  287        2769.0         8166.0             9722.0\n",
      "3    1/2/2015  286        2769.0         8157.0             9722.0\n",
      "4  12/31/2014  284        2730.0         8115.0             9633.0\n",
      "5  12/28/2014  281        2706.0         8018.0             9446.0\n",
      "6  12/27/2014  280        2695.0         8018.0             9409.0\n",
      "7  12/24/2014  277        2630.0         7977.0             9203.0\n",
      "8  12/21/2014  273        2597.0         7977.0             9004.0\n",
      "9  12/20/2014  272        2571.0         7862.0             8939.0\n",
      "         Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone\n",
      "0    1/5/2015  289        2776.0            NaN            10030.0\n",
      "1    1/4/2015  288        2775.0            NaN             9780.0\n",
      "2    1/3/2015  287        2769.0         8166.0             9722.0\n",
      "3    1/2/2015  286        2749.5         8157.0             9677.5\n",
      "4  12/31/2014  284        2730.0         8115.0             9633.0\n",
      "5  12/28/2014  281        2706.0         8018.0             9446.0\n",
      "6  12/27/2014  280        2695.0         7997.5             9409.0\n",
      "7  12/24/2014  277        2630.0         7977.0             9203.0\n",
      "8  12/21/2014  273        2597.0         7919.5             9004.0\n",
      "9  12/20/2014  272        2571.0         7862.0             8939.0\n"
     ]
    }
   ],
   "source": [
    "print(ebola.fillna(method = 'ffill').iloc[0:10, 0:5])\n",
    "print(ebola.interpolate().iloc[0:10, 0:5])\n",
    "# 通过结果可以观察到，无论是使用前值填充还是插值填充，都可能无法对所有的缺失值进行处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone  \\\n",
      "19  11/18/2014  241        2047.0         7082.0             6190.0   \n",
      "\n",
      "    Cases_Nigeria  Cases_Senegal  Cases_UnitedStates  Cases_Spain  Cases_Mali  \\\n",
      "19           20.0            1.0                 4.0          1.0         6.0   \n",
      "\n",
      "    Deaths_Guinea  Deaths_Liberia  Deaths_SierraLeone  Deaths_Nigeria  \\\n",
      "19         1214.0          2963.0              1267.0             8.0   \n",
      "\n",
      "    Deaths_Senegal  Deaths_UnitedStates  Deaths_Spain  Deaths_Mali  \n",
      "19             0.0                  1.0           0.0          6.0  \n"
     ]
    }
   ],
   "source": [
    "ebola_dropna = ebola.dropna()\n",
    "print(ebola_dropna)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          Date  Day  Cases_Guinea  Cases_Liberia  Cases_SierraLeone  \\\n",
      "13   12/7/2014  260        2292.0         7758.0             7897.0   \n",
      "14   12/3/2014  256        2228.0         7719.0             7604.5   \n",
      "15  11/30/2014  253        2164.0         7677.0             7312.0   \n",
      "16  11/28/2014  251        2149.0         7635.0             6955.5   \n",
      "17  11/23/2014  246        2134.0         7401.5             6599.0   \n",
      "\n",
      "    Cases_Nigeria  Cases_Senegal  Cases_UnitedStates  Cases_Spain  Cases_Mali  \\\n",
      "13           20.0            1.0                 4.0          1.0         7.0   \n",
      "14           20.0            1.0                 4.0          1.0         7.0   \n",
      "15           20.0            1.0                 4.0          1.0         7.0   \n",
      "16           20.0            1.0                 4.0          1.0         7.0   \n",
      "17           20.0            1.0                 4.0          1.0         7.0   \n",
      "\n",
      "    Deaths_Guinea  Deaths_Liberia  Deaths_SierraLeone  Deaths_Nigeria  \\\n",
      "13         1428.0          3233.5              1768.0             8.0   \n",
      "14         1377.5          3177.0              1675.5             8.0   \n",
      "15         1327.0          3161.0              1583.0             8.0   \n",
      "16         1293.5          3145.0              1490.5             8.0   \n",
      "17         1260.0          3080.5              1398.0             8.0   \n",
      "\n",
      "    Deaths_Senegal  Deaths_UnitedStates  Deaths_Spain  Deaths_Mali  \n",
      "13             0.0                  1.0           0.0          6.0  \n",
      "14             0.0                  1.0           0.0          6.0  \n",
      "15             0.0                  1.0           0.0          6.0  \n",
      "16             0.0                  1.0           0.0          6.0  \n",
      "17             0.0                  1.0           0.0          6.0  \n"
     ]
    }
   ],
   "source": [
    "#   先使用线性插值处理大部分的缺失值，然后再利用删除剩余的缺失数据\n",
    "ebola_inter = ebola.interpolate()\n",
    "ebola_inter_drop = ebola_inter.dropna()\n",
    "print(ebola_inter_drop.head())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
